/************************************************************************************/
/*  Program name CAPGLIMP_AUTOREV.sql                                        	    */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 27 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_AUTOREV';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_AUTOREVERSE_OPTIONS;
		
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO GL_AUTOREVERSE_OPTIONS
			VALUES
			(
			CAPBRIMP_MAPPING.GetSOBIDMapping(C1.SET_OF_BOOKS_ID),
			C1.JE_CATEGORY_NAME,
			C1.METHOD_CODE,
			C1.REVERSAL_PERIOD_CODE,
			C1.AUTOREVERSE_FLAG,
			C1.AUTOPOST_REVERSAL_FLAG,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.LAST_UPDATE_LOGIN,
			C1.REVERSAL_DATE_CODE,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.CONTEXT     
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/